package com.ruoyi.wzgl.wzpd.controller;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.wzgl.wzrk.domain.Wzrk;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import java.io.InputStream;

import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.wzgl.wzpd.domain.Wzpd;
import com.ruoyi.wzgl.wzpd.service.IWzpdService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物资盘点Controller
 *
 * @author huacai
 * @date 2024-09-28
 */
@RestController
@RequestMapping("/wzgl/wzpd")
public class WzpdController extends BaseController {
    @Resource
    private IWzpdService wzpdService;

    /**
     * 查询物资盘点列表
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzpd:list')")
    @GetMapping("/list")
    public TableDataInfo list(Wzpd wzpd) {
        startPage();
        List<Wzpd> list = wzpdService.selectWzpdList(wzpd);
        return getDataTable(list);
    }

    /**
     * 导出物资盘点列表
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzpd:export')")
    @Log(title = "物资盘点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Wzpd wzpd) {
        List<Wzpd> list = wzpdService.selectWzpdList(wzpd);
        List<Wzpd> resultList = new ArrayList<>();
        for(Wzpd item : list){
            Wzpd result = wzpdService.selectWzpdByWzpdId(item.getWzpdId());
            result.setWzpdPdmxList(result.getWzpdPdmxList()); // 设置查询到的子表数据到主表对象中
            resultList.add(result); // 将查询结果添加到结果集合中
        }
        ExcelUtil<Wzpd> util = new ExcelUtil<Wzpd>(Wzpd.class);
        util.exportExcel(response, resultList, "物资盘点数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Wzpd> util = new ExcelUtil<Wzpd>(Wzpd.class);
        util.importTemplateExcel(response, "物资盘点数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "物资盘点", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('wzgl:wzpd:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Wzpd> util = new ExcelUtil<Wzpd>(Wzpd.class);
        InputStream inputStream = file.getInputStream();
        List<Wzpd> list = util.importExcel(inputStream);
        inputStream.close();
        int count = wzpdService.batchInsertWzpd(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取物资盘点详细信息
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzpd:query')")
    @GetMapping(value = "/{wzpdId}")
    public AjaxResult getInfo(@PathVariable("wzpdId") Integer wzpdId) {
        return success(wzpdService.selectWzpdByWzpdId(wzpdId));
    }

    /**
     * 新增物资盘点
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzpd:add')")
    @Log(title = "物资盘点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Wzpd wzpd) {
        wzpd.setUserId(Math.toIntExact(getUserId()));
        wzpd.setDeptId(Math.toIntExact(getDeptId()));
        wzpd.setCreateBy(getUsername());
        return toAjax(wzpdService.insertWzpd(wzpd));
    }

    /**
     * 修改物资盘点
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzpd:edit')")
    @Log(title = "物资盘点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Wzpd wzpd) {
        return toAjax(wzpdService.updateWzpd(wzpd));
    }

    /**
     * 删除物资盘点
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzpd:remove')")
    @Log(title = "物资盘点", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wzpdIds}")
    public AjaxResult remove(@PathVariable Integer[] wzpdIds) {
        return toAjax(wzpdService.deleteWzpdByWzpdIds(wzpdIds));
    }
}
